package cn.tedu.charging.perm.contoller;

import cn.tedu.charging.common.pojo.response.JsonResult;
import cn.tedu.charging.perm.service.PermService;

import cn.tedu.charging.perm.pojo.param.PermQuery;
import cn.tedu.charging.perm.pojo.param.PermSaveParam;
import cn.tedu.charging.perm.pojo.po.RolePerm;
import cn.tedu.charging.perm.pojo.vo.PermVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/perm")
@Api(tags = "权限管理")
@Slf4j
public class PermController {
    @Autowired
    PermService permService;
    @ApiOperation("查询权限接口")
    @GetMapping("select")
    public JsonResult selectPerm(PermQuery permQuery) {
      log.debug("查询权限接口的入参为{}",permQuery);
      log.debug("查询接口调用业务层的入参为{}",permQuery);
      List<PermVO> list = permService.selectPerm(permQuery);
      log.debug("查询接口调用业务层的入参为{}出参为{}",permQuery,list);
        return JsonResult.ok(list);
    }
    @ApiOperation("保存权限接口")
    @PostMapping("save")
    public JsonResult savePerm(@RequestBody PermSaveParam permSaveParam) {
        log.debug("保存权限接口的入参为{}",permSaveParam);
        log.debug("保存接口调用业务层的入参为{}",permSaveParam);
        permService.savePerm(permSaveParam);
        JsonResult jsonResult = JsonResult.ok("成功保存权限");
        log.debug("保存接口调用业务层的入参为{}出参{}",permSaveParam,jsonResult);
        return jsonResult;
    }
    @ApiOperation("删除权限接口")
    @PostMapping("delete/{permId}")
    public JsonResult deletePerm(@PathVariable Long permId) {
        log.debug("删除权限接口的入参为{}",permId);
        log.debug("删除接口调用业务层的入参为{}",permId);
        permService.deletePerm(permId);
        JsonResult jsonResult = JsonResult.ok("成功删除权限");
        log.debug("删除接口调用业务层的入参为{}出参{}",permId,jsonResult);
        return jsonResult;
    }
    @ApiOperation("添加权限角色关联接口")
    @PostMapping("addRolePerm/{roleId}/{permId}")
    public JsonResult addRolePerm(@PathVariable Long roleId,@PathVariable Long permId) {
        log.debug("添加权限角色关联接口的入参为角色Id{}权限Id{}",roleId,permId);
        log.debug("添加权限角色关联接口调用业务层的入参为角色Id{}权限Id{}",roleId,permId);
        permService.addRolePerm(roleId,permId);
        JsonResult jsonResult = JsonResult.ok("成功添加权限角色关联");
        log.debug("添加权限角色关联接口调用业务层的入参为角色Id{}权限Id{}出参{}",roleId,permId,jsonResult);
        return jsonResult;
    }
    @ApiOperation("删除权限角色关联接口")
    @PostMapping("deleteRolePerm/{roleId}/{permId}")
    public JsonResult deleteRolePerm(@PathVariable Long roleId,@PathVariable Long permId) {
        log.debug("删除权限角色关联接口的入参为角色Id{}权限Id{}",roleId,permId);
        log.debug("删除权限角色关联接口调用业务层的入参为角色Id{}权限Id{}",roleId,permId);
        permService.deleteRolePerm(roleId,permId);
        JsonResult jsonResult = JsonResult.ok("成功删除权限角色关联");
        log.debug("删除权限角色关联接口调用业务层的入参为角色Id{}权限Id{}出参{}",roleId,permId,jsonResult);
        return jsonResult;
    }
    @ApiOperation("查询角色权限关联接口")
    @GetMapping("selectRolePerm")
    public JsonResult selectRolePerm(@RequestBody RolePerm rolePerm) {
        log.debug("查询角色权限关联接口的入参为{}",rolePerm);
        log.debug("查询角色权限关联接口调用业务层的入参为{}",rolePerm);
        List<RolePerm> list = permService.selectRolePerm(rolePerm);
        JsonResult jsonResult = JsonResult.ok(list,"成功查询角色权限关联");
        log.debug("查询角色权限关联接口调用业务层的入参为{}出参为{}",rolePerm,jsonResult);
        return jsonResult;
    }
    @ApiOperation("查询此角色未绑定的权限接口")
    @GetMapping("selectUnBindPerm")
    public JsonResult selectUnBindPerm(Long roleId) {
    log.debug("查询此角色未绑定的权限接口的入参为{}",roleId);
    log.debug("查询此角色未绑定的权限接口调用业务层的入参为{}",roleId);
    List<PermVO> list = permService.selectUnBindPerm(roleId);
        JsonResult jsonResult = JsonResult.ok(list,"成功查询此角色未绑定的权限");
    log.debug("查询此角色未绑定的权限接口调用业务层的入参为{}出参为{}",roleId,jsonResult);
    return jsonResult;
    }
}
